package system

import (
	"context"
	"gf-vben-admin/consts"
	model "gf-vben-admin/internal/model/system"
	"gf-vben-admin/internal/model/system/entity"
	"gf-vben-admin/internal/service/system/internal/dao"
	utils "gf-vben-admin/utility"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
)

type (
	sPermission struct{}
)

var (
	insPermission = sPermission{}
)

func Permission() *sPermission {
	return &insPermission
}

func (s *sPermission) PageList(ctx context.Context, param model.Permission) (total int, result []*entity.Permission, err error) {
	result = make([]*entity.Permission, 0)
	daoModel := dao.Permission.Ctx(ctx)
	columnMap := dao.Permission.ColumnMap()
	daoModel, err = utils.GetWrapper(param, daoModel, columnMap)
	if err != nil {
		return
	}
	total, err = daoModel.Count()
	if err != nil {
		g.Log().Error(ctx, err)
		err = gerror.New("获取总行数失败")
		return
	}
	if total == 0 {
		return
	}
	err = daoModel.Page(param.PageNum, param.PageSize).Scan(&result)
	if err != nil {
		g.Log().Error(ctx, err)
		err = gerror.New("获取数据失败")
	}
	return
}

func (s *sPermission) List(ctx context.Context, param entity.Permission) (result []*entity.Permission, err error) {
	result = make([]*entity.Permission, 0)
	daoModel := dao.Permission.Ctx(ctx)
	columnMap := dao.Permission.ColumnMap()
	daoModel, err = utils.GetWrapper(param, daoModel, columnMap)
	if err != nil {
		return
	}
	err = daoModel.Scan(&result)
	if err != nil {
		g.Log().Error(ctx, err)
		err = gerror.New("获取数据失败")
	}
	return
}

// Get 查询API表详情
func (s *sPermission) Get(ctx context.Context, id int64) (result *entity.Permission, err error) {
	result = new(entity.Permission)
	daoModel := dao.Permission.Ctx(ctx)
	err = daoModel.Where(dao.Permission.Columns().Id, id).Scan(&result)
	if err != nil {
		g.Log().Error(ctx, err)
		err = gerror.New("获取数据失败")
	}
	return
}

func (s *sPermission) Add(ctx context.Context, in model.Permission) (err error) {
	err = dao.Permission.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		_, err := tx.Model(dao.Permission.Table()).Ctx(ctx).OmitEmpty().Data(in).Insert()
		return err
	})
	return
}

func (s *sPermission) Edit(ctx context.Context, in model.Permission) (err error) {
	err = dao.Permission.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		_, err := tx.Model(dao.Permission.Table()).Ctx(ctx).OmitEmpty().Data(in).Where(g.Map{
			dao.Permission.Columns().Id: in.Id,
			consts.VersionNumberColumn:  in.VersionNumber,
		}).Update()
		return err
	})
	return
}

func (s *sPermission) EditState(ctx context.Context, ids []int64, state int8) (err error) {
	err = dao.Permission.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		_, err := tx.Model(dao.Permission.Table()).Ctx(ctx).Data(g.Map{consts.StateColumn: state}).Where(g.Map{
			dao.Permission.Columns().Id: ids,
		}).Update()
		return err
	})
	return
}

func (s *sPermission) Delete(ctx context.Context, ids []int64) (err error) {
	err = dao.Permission.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		_, err := tx.Model(dao.Permission.Table()).Ctx(ctx).Where(g.Map{
			dao.Permission.Columns().Id: ids,
		}).Delete()
		return err
	})
	return
}

func (s *sPermission) InitPermission(ctx context.Context, list []entity.Permission) (err error) {
	code := make([]string, 0)
	for _, permission := range list {
		code = append(code, permission.Code)
	}

	err = dao.Permission.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		//删除已移除的API关联权限
		_, err = tx.Model(dao.RolePermission.Table()).Ctx(ctx).WhereNotIn(dao.RolePermission.Columns().PermissionCode, code).Delete()
		//删除已移除的API
		_, err = tx.Model(dao.Permission.Table()).Ctx(ctx).WhereNotIn(dao.Permission.Columns().Code, code).Delete()
		//保存
		_, err = tx.Model(dao.Permission.Table()).Ctx(ctx).OmitEmpty().FieldsEx(dao.Permission.Columns().Id).Data(list).Save()
		return err
	})
	return
}
